import { mapState, mapMutations } from 'vuex'
import { getBirthday,getNewToken,Trim,gender } from '../../config/util'
import { passengerMessVerificate } from '../../config/messVerify'
export default {
	name: 'addPassenger',
	data() {
		return {
			success: false,
			failed: false,
			type: false, 
			passenger: '1', //旅客类型默认为成人
			certificateDefault: '', 
			sex: "1", //默认为男
			chineseName: "", //国内姓名字段
			docId: "", //证件号
			docExpireDate: "", //证件有效期
			mobile: "", //国内乘机人的手机号码
			sureName: "",
			givenName: "",
			nationality: "",
			birthDate: "1990-01-01",
			inlandMobile: '',
			errorMsg:'添加失败',
		}
	},
	computed: {
		...mapState([
			'isInland', 'Date','token'
		]),
		certificate() {
			if(!this.type) this.certificateDefault = '1';
			else{
				if(this.certificateDefault=='1'){
					this.certificateDefault = '2'					
				}				
			}
			return this.certificateDefault;
		}
	},
	watch:{
		failed:function(val){
			if(val){
				var that = this;
				setTimeout(function(){
					that.failed = false;
				},5000)
			}
		},
		
	},
	methods: {
		...mapMutations([
			'RECORD_TOKEN'
		]),
		passengerType(value) {
			this.passenger = value
		},
		certificateType(value) {
			this.certificateDefault = value;
			if(value == "1") this.type = false;
			else this.type = true;
		},
		getParams() {
			var data;
			if(this.type) {
				this.sureName = (this.sureName).toUpperCase()
				this.givenName = (this.givenName).toUpperCase()
				data = {
					"firstNameCn": Trim(this.sureName +"/"+ this.givenName),
					"passengerType": this.passenger,
					"docType": this.certificateDefault,
					"docId": Trim(this.docId),
					"docExpireDate": this.docExpireDate,
					"mobile": Trim(this.inlandMobile),
					"birthDate": this.birthDate,
					"fromDate": this.Date.postDepDate,
					"sex": this.sex,
					"nationality": Trim(this.nationality),
					"lastName_cn": Trim(this.sureName +"/"+ this.givenName),
					"lastNameEn":Trim(this.givenName),
					"flightType": this.isInland ? "1" : "2",
				};				
			} else {
				data = {
					"firstNameCn": Trim(this.chineseName),
					"lastName_cn": Trim(this.chineseName),
					"passengerType": this.passenger,
					"docType": this.certificateDefault,
					"docId": Trim(this.docId),
					"birthDate": getBirthday(this.docId),
					"mobile": Trim(this.mobile),
					"flightType": this.isInland ? "1" : "2",
					"fromDate": this.Date.postDepDate,
					"sex": gender(this.docId)
				};
			}	
			return data;
		},
		create() {		
			var data = this.getParams();//获取参数		
			var result = passengerMessVerificate(data);//校验
			if(!result.status){         //如果校验结果有误，展示原因 阻止请求
				this.errorMsg = result.msg;
				this.failed = true;
				return false;
			}	
			this.$axios({
					method: 'post',
					url: '/passenger/create?temp=' + Math.random(),
					data: data,
					headers: {'authorization':this.token}
				})
				.then((res) => {
					this.RECORD_TOKEN({type:"update",val:getNewToken(res.headers)});
					var success = res.data.success;
					var msg = res.data.msg;
					this.handResult(success,msg);
				})
				.catch((error) => {
					alert("请求异常")
				})
		},
		handResult(success,msg) {
			var that = this;
			if(success) {
				this.success = true;
				this.failed = false;
				setTimeout(function() {
					that.$router.push({ path: '/book' });
				}, 1000)

			} else {
				this.errorMsg = msg;
				this.success = false;
				this.failed = true;
			}
		}
	},
	created() {
		document.title = "新增乘机人";
		//国际航班 只能添加国际乘机人
		if(!this.isInland){
			this.type = true;
			this.certificateDefault = '2';
		}
	}
}